package com.rjwh.dingdong.client.util;

import android.media.MediaPlayer;

/**
 * @author Kay
 * @ClassName MediaPlayUtil
 * @Description
 * @date 2016-8-19
 * @history 2016-8-19 author: description:
 */
public class MediaPlayUtil {

    // 声音播放相关的变量
    public MediaPlayer mediaPlayer;

    /**
     * 播放声音
     *
     * @param audioFileName 文件路径
     * @param isLoop        是否循环
     */
    public void playAudio(String audioFileName, boolean isLoop) {
        // 检查入参
        if (null == audioFileName || audioFileName.length() <= 0) {
            return;
        }
        // 创建播放器
        if (null == mediaPlayer) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.reset();
        }

//        mediaPlayer.stop();
        // 播放完成后的回调
        MediaPlayer.OnCompletionListener onCompletionListener = new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                // 停止播放器
                if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                }
                // 释放资源
                if (mediaPlayer != null) {
                    mediaPlayer.reset();
                    mediaPlayer.release();
                    mediaPlayer = null;
                }
                System.gc();
            }
        };


        // 开始播放
        try {

            mediaPlayer.reset();
            mediaPlayer.setDataSource(audioFileName);
            mediaPlayer.setLooping(isLoop);
            mediaPlayer.setOnCompletionListener(onCompletionListener);
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
            mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    mediaPlayer.reset();
                    return false;
                }
            });
            mediaPlayer.prepareAsync();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 停止声音
     */
    public void stop() {

        try {
            if (mediaPlayer != null) {
                mediaPlayer.stop();
                mediaPlayer.reset();
                mediaPlayer.release();
                mediaPlayer = null;
                System.gc();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 继续播放
     */
    public void resume() {
        try {
            if (mediaPlayer != null) {
                mediaPlayer.start();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 暂停
     */
    public void pause() {
        try {

            if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
